package ejbcareader.utils;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import ejbcareader.reader.pojo.CertificatePojo;

public class FileHelper {
	
	private static Logger log = Logger.getLogger(FileHelper.class);
	
	public static void Write(CertificatePojo pojo, String filename){
		ArrayList<CertificatePojo> list = new ArrayList<CertificatePojo>();
		list.add(pojo);
		Write(list, filename);
	}
	
	//TODO: Isto tem de ser generico!
	public static void Write(List<CertificatePojo> pojos, String filename)
	{							
		try
        {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), "UTF-8"));
            for (CertificatePojo o : pojos)
            {
                bw.write(o.toString());
                bw.newLine();
            }
            bw.flush();
            bw.close();
        } catch (Exception ex) {
            log.error("Error saving file", ex);
        } 
		
		// Temos de chamar o dispose aqui?? ou ele resolve-se sozinho?
	}
}
